package com.bwie.user.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;


import com.bwie.common.vo.WeiboAccessVo;
import com.bwie.user.config.JwtUtil;
import com.bwie.user.config.MsgUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bwie.user.entity.UserEntity;
import com.bwie.user.service.UserService;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.R;



/**
 * 
 *
 * @author wangguohai
 * @email 270901876@qq.com
 * @date 2022-04-21 20:41:37
 */
@RestController
@RequestMapping("user/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private JwtUtil jwtUtil;

    /**
    * @Author: 龍
    * @Description: 微博登录注册
    * @DateTime: 2022/5/4 14:28
    * @Params: 
    * @Return: 
    */
    @RequestMapping("/weiblogin")
    public R weiboLogin(@RequestBody WeiboAccessVo weiboAccessVo) throws Exception {
        return userService.weiboLogin(weiboAccessVo);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{uid}")
    public R info(@PathVariable("uid") Integer uid){
		UserEntity user = userService.getById(uid);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserEntity user){
		userService.save(user);

        return R.ok();
    }

    @RequestMapping("/register")
    public R register(@RequestBody UserEntity user){
		userService.register(user);

        return R.ok();
    }

    @RequestMapping("/getCode/{mobile}")
    public R getCode(@PathVariable("mobile") String mobile){
        //先去判断是否锁定
        String s1 = redisTemplate.opsForValue().get(mobile);
        if (StringUtils.isEmpty(s1)){
            String num="0";
            //获取他的短期的登录次数
            if(redisTemplate.opsForValue().get(mobile + "-num")!=null){
                num = redisTemplate.opsForValue().get(mobile + "-num");
            }
            if (Integer.valueOf(num)<3){
                String s = MsgUtil.sendMsg(mobile);
                redisTemplate.opsForValue().set(mobile+"-code",s);

                redisTemplate.opsForValue().set(mobile + "-num",(Integer.valueOf(num)+1)+"",1,TimeUnit.MINUTES);
                return R.ok();
            }else {
                redisTemplate.opsForValue().set(mobile,"1",3, TimeUnit.MINUTES);

                return R.error().put("msg","短信发送过多");
            }
        }else {
            return R.error().put("msg","账号已被锁定，请3分钟后登录");
        }
    }

    @RequestMapping("/getUser")
    public UserEntity getUser(@RequestParam("userId") Integer userId){
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        UserEntity byId = userService.getById(userId);
        if (byId!=null){
            byId.setCode("");
        }

        return byId;

    }




     @RequestMapping("/login")
    public R login(@RequestBody UserEntity user){
         String code = user.getCode();
         String s = redisTemplate.opsForValue().get(user.getMobile() + "-code");
         if (s.equals(code)){
             UserEntity userEntity=userService.login(user);
             //已注册过
             if (userEntity!=null){
                 userEntity.setLogintime(new Date());
                 userService.updateById(userEntity);
                 String token = jwtUtil.createJWT(userEntity.getUid() + "", userEntity.getUname(), "admin");

                 return R.ok().put("msg","1").put("token",token).put("user",userEntity);
             }else {
                 //去注册
                 return R.ok().put("msg","2");
             }
         }
         return R.error().put("msg","验证码错误");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] uids){
		userService.removeByIds(Arrays.asList(uids));

        return R.ok();
    }

}
